package uk.co.nazz.f21mc_a2.utils;

import uk.co.nazz.f21mc_a2.R;
import uk.co.nazz.f21mc_a2.diary.Event;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * @author Andrew Stokes
 * @extends ArrayAdapter an adapter for the events list ListView to display text
 *          and icons for events
 * 
 * 
 */
public class ExtendedArrayAdapter extends ArrayAdapter<Event> {
	private final Context context;
	private final Event[] events;

	public ExtendedArrayAdapter(Context context, Event[] events) {
		super(context, R.layout.events_list_item, events);
		this.context = context;
		this.events = events;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View itemView = inflater.inflate(R.layout.events_list_item, parent,
				false);
		// set the text view of the list item to be the toString result of the
		// event
		TextView textView = (TextView) itemView
				.findViewById(R.id.event_item_textview);

		ImageView recurringIcon = (ImageView) itemView
				.findViewById(R.id.event_item_recurring_icon);
		ImageView attendeeIcon = (ImageView) itemView
				.findViewById(R.id.event_item_attendees_icon);
		textView.setText(events[position].toString());

		// set images for the recurrence status of the list
		if (events[position].getIsRecurring()) {
			// display recurring icon
			recurringIcon.setImageResource(R.drawable.recurring);
		} else {
			// display single event icon
			recurringIcon.setImageResource(R.drawable.non_recurring);
		}

		// display an icon for the attendees
		int attendeeCount = events[position].getAttendeeCount();
		switch (attendeeCount) {
		case 2:
			// two attendee icon
			attendeeIcon.setImageResource(R.drawable.two_attendees);
			break;
		case 1:
			// one attendee icon
			attendeeIcon.setImageResource(R.drawable.one_attendee);
			break;
		case 0:
			// empty attendee icon
			attendeeIcon.setImageResource(R.drawable.no_attendees);
			break;
		default:
			// three attendee icon
			attendeeIcon.setImageResource(R.drawable.many_attendees);
			break;
		}

		return itemView;
	}
}
